# This file is part of GammaRay, the Qt application inspection and manipulation tool.
#
# SPDX-FileCopyrightText: 2013 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#

# generate the class icon index map
file(READ "../ui/resources/classiconindex.map" _map_content)
string(REPLACE "\n" ";" _map_content ${_map_content})
set(CLASS_ICON_INDEX_CODE "")
foreach(_icon_data IN LISTS _map_content)
    if(_icon_data)
        string(REPLACE " " ";" _icon_data ${_icon_data})
        list(GET _icon_data 0 _icon_name)
        set(CLASS_ICON_INDEX_CODE "${CLASS_ICON_INDEX_CODE}\n    ADD_ICON(\"${_icon_name}\")")
    endif()
endforeach()
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/classesiconsindex_data.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/classesiconsindex_data.cpp
)

add_library(
    gammaray_lz4 OBJECT
    ${CMAKE_SOURCE_DIR}/3rdparty/lz4/lz4.c
)
set_target_properties(gammaray_lz4 PROPERTIES POSITION_INDEPENDENT_CODE ON)
#
# Shared non-UI code between probe and client
#
set(gammaray_common_srcs
    classesiconsrepository.cpp
    classesiconsrepository.h
    commonutils.cpp
    commonutils.h
    endpoint.cpp
    endpoint.h
    enumdefinition.cpp
    enumdefinition.h
    enumrepository.cpp
    enumrepository.h
    enumvalue.cpp
    enumvalue.h
    message.cpp
    message.h
    methodargument.cpp
    methodargument.h
    modelevent.cpp
    modelevent.h
    modelutils.cpp
    modelutils.h
    objectbroker.cpp
    objectbroker.h
    objectid.h
    objectidfilterproxymodel.cpp
    objectidfilterproxymodel.h
    paintanalyzerinterface.cpp
    paintanalyzerinterface.h
    paths.cpp
    paths.h
    propertysyncer.cpp
    propertysyncer.h
    protocol.cpp
    protocol.h
    remoteviewframe.cpp
    remoteviewframe.h
    remoteviewinterface.cpp
    remoteviewinterface.h
    selflocator.cpp
    selflocator.h
    sourcelocation.cpp
    sourcelocation.h
    transferimage.cpp
    transferimage.h
    translator.cpp
    translator.h
)

add_library(
    gammaray_common
    ${GAMMARAY_LIBRARY_TYPE} ${gammaray_common_srcs}
)
generate_export_header(gammaray_common)
# cmake-lint: disable=E1120
set_target_properties(
    gammaray_common PROPERTIES ${GAMMARAY_DEFAULT_LIBRARY_PROPERTIES} OUTPUT_NAME gammaray_common-${GAMMARAY_PROBE_ABI}
)
target_compile_features(gammaray_common PUBLIC ${GAMMARAY_REQUIRED_CXX_FEATURES})

gammaray_target_relocatable_interfaces(gammaray_common_ipaths)
target_include_directories(
    gammaray_common
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> $<INSTALL_INTERFACE:${gammaray_common_ipaths}>
)
target_link_libraries(
    gammaray_common
    PUBLIC Qt::Core
    PRIVATE Qt::Network Qt::Gui Qt::GuiPrivate
)
target_link_libraries(
    gammaray_common
    PRIVATE $<BUILD_INTERFACE:gammaray_lz4> gammaray_kitemmodels ${CMAKE_DL_LIBS}
)
gammaray_set_rpath(gammaray_common ${LIB_INSTALL_DIR})
if(GAMMARAY_USE_PCH)
    target_precompile_headers(gammaray_common REUSE_FROM gammaray_pch_core_gui)
endif()

set(gammaray_common_internal_srcs
    favoriteobjectinterface.cpp
    favoriteobjectinterface.h
    networkselectionmodel.cpp
    networkselectionmodel.h
    plugininfo.cpp
    plugininfo.h
    pluginmanager.cpp
    pluginmanager.h
    probecontrollerinterface.cpp
    probecontrollerinterface.h
    propertycontrollerinterface.cpp
    propertycontrollerinterface.h
    proxyfactorybase.cpp
    proxyfactorybase.h
    streamoperators.cpp
    streamoperators.h
    toolmanagerinterface.cpp
    toolmanagerinterface.h
    tools/messagehandler/messagehandlerinterface.cpp
    tools/messagehandler/messagehandlerinterface.h
    tools/metatypebrowser/metatypebrowserinterface.cpp
    tools/metatypebrowser/metatypebrowserinterface.h
    tools/objectinspector/connectionsextensioninterface.cpp
    tools/objectinspector/connectionsextensioninterface.h
    tools/objectinspector/methodsextensioninterface.cpp
    tools/objectinspector/methodsextensioninterface.h
    tools/objectinspector/propertiesextensioninterface.cpp
    tools/objectinspector/propertiesextensioninterface.h
    tools/problemreporter/problemreporterinterface.cpp
    tools/problemreporter/problemreporterinterface.h
    tools/resourcebrowser/resourcebrowserinterface.cpp
    tools/resourcebrowser/resourcebrowserinterface.h
)

add_library(
    gammaray_common_internal OBJECT
    ${gammaray_common_internal_srcs}
)
set_target_properties(gammaray_common_internal PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_features(gammaray_common_internal PUBLIC ${GAMMARAY_REQUIRED_CXX_FEATURES})

target_link_libraries(
    gammaray_common_internal gammaray_common
)
target_link_libraries(
    gammaray_common_internal Qt::Core Qt::Network
)

if(NOT GAMMARAY_PROBE_ONLY_BUILD)
    install(
        TARGETS gammaray_common
        EXPORT GammaRayTargets
        ${INSTALL_TARGETS_DEFAULT_ARGS}
    )

    gammaray_install_headers(
        ${CMAKE_CURRENT_BINARY_DIR}/gammaray_common_export.h
        endpoint.h
        enumdefinition.h
        enumrepository.h
        enumvalue.h
        metatypedeclarations.h
        modelroles.h
        objectbroker.h
        objectid.h
        objectmodel.h
        objectidfilterproxymodel.h
        paths.h
        probecontrollerinterface.h
        propertycontrollerinterface.h
        protocol.h
        sourcelocation.h
        translator.h
        commonutils.h
        favoriteobjectinterface.h
    )

    ecm_generate_pri_file(
        BASE_NAME
        GammaRayCommon
        LIB_NAME
        gammaray_common-${GAMMARAY_PROBE_ABI}
        DEPS
        "core gui network"
        FILENAME_VAR
        PRI_FILENAME
        INCLUDE_INSTALL_DIR
        ${INCLUDE_INSTALL_DIR}
    )

    install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR})
else()
    install(TARGETS gammaray_common ${INSTALL_TARGETS_DEFAULT_ARGS})
endif()
if(MSVC)
    install(
        FILES "$<TARGET_PDB_FILE_DIR:gammaray_common>/$<TARGET_PDB_FILE_NAME:gammaray_common>"
        DESTINATION ${BIN_INSTALL_DIR}
        CONFIGURATIONS Debug RelWithDebInfo
    )
endif()
